package helpers;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CrawlerCountry {
	public static void main(String[] args) {
		String url = "http://countrycode.org";
		String url2 = "http://itunes.apple.com/rss?cc=VN";
		try {
			Document doc = Jsoup.connect(url).get();
			Elements els = doc.select("#main_table_blue tbody tr");
			
			Document doc2 = Jsoup.connect(url2).get();
			Elements els2 = doc2.select("#feedCountry option");
			
			if (els.size() > 0) {
				Element tr = null;
				String country = "";
				String iosCode2Digit = "";
				String iosCode3Digit = "";
				String telephoneCode = "";
				String[] str = new String[2];
				List<String> str2 = new ArrayList<String>();
				for (int i = 0; i < els2.size(); i++) {
					str2.add(els2.get(i).val());
				}
				
				for (int i = 0; i< els.size(); i++) {
					tr = els.get(i);
					country = tr.select("td").get(0).select("a").html();
					str = tr.select("td").get(1).html().trim().split("/");
					iosCode2Digit = str[0].trim();
					iosCode3Digit = str[1].trim().replace("&nbsp;", "");
					telephoneCode = tr.select("td").get(2).html().replace("&nbsp;", "");
					if (str2.contains(iosCode2Digit)) {
						System.out.println("//Set country " + country); 
						System.out.println("country = new Country();");
						System.out.println("country.setName(\""+ country + "\");");
						System.out.println("country.setIosCode2Digit(\"" + iosCode2Digit + "\");");
						System.out.println("country.setIosCode3Digit(\"" + iosCode3Digit + "\");");
						System.out.println("country.setTelephoneCode(\""+ telephoneCode +"\");");
						System.out.println("lstCountries.add(country);");
						System.out.println("");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
	}
}
